/**
 * ServiceLocator.java
 * Created 28 Sep 2011
 */
package com.fastwire.alarm.common.view.servicelocator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.fastwire.alarm.admin.model.service.AlGroupMenuService;
import com.fastwire.alarm.common.view.constant.DefaultValues;

/**
 * @author sigit <br>
 * @version 1.0 <br>
 * Application : alarmmanagement <br>
 * Copyright (c) fastwire-group 2011 <br>
 * <br>
 */
public class ServiceLocator {

	//This class logger
	private Log log = LogFactory.getLog(ServiceLocator.class);
	
	//Application Context
	private ApplicationContext applicationContext;
	private static ServiceLocator serviceLocator = new ServiceLocator();
	
	private boolean servicesStored = false;
	
	
	private AlGroupMenuService alGroupMenuService;
		

	private ServiceLocator() {
		// TODO Auto-generated constructor stub
		log.debug("@alarm>Create serviceLocator object.");
		this.applicationContext = new ClassPathXmlApplicationContext(DefaultValues.APPLICATION_CONTEXT_PATH);
		if (isServicesStored()) {
			//Store all service 
		}
	}
	
	public static ServiceLocator getInstance() {
		if (serviceLocator == null) {
			serviceLocator = new ServiceLocator();
		}
		
		return serviceLocator;
	}

	private void storeAllServices() {
		
	}
	
	/**
	 * Lookup service based on service bean name.
	 * refer to ServiceBeanNames class
	 * 
	 * @param serviceBeanName
	 *            the service bean name
	 * @return the service bean
	 */
	public Object lookupServiceBean(String serviceBeanName) {
		return applicationContext.getBean(serviceBeanName);
	}
	
	
	public ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	public AlGroupMenuService getAlGroupMenuService() {
		if (isServicesStored()) {
			return alGroupMenuService;
		} else {
			return (AlGroupMenuService) lookupServiceBean(ServiceBeanName.GROUP_MENU_SERVICE);			
		}
		
	}

	public void setAlGroupMenuService(AlGroupMenuService alGroupMenuService) {
		this.alGroupMenuService = alGroupMenuService;
	}

	public boolean isServicesStored() {
		return servicesStored;
	}

	public void setServiceStored(boolean servicesStored) {
		if (!this.servicesStored && servicesStored)
			storeAllServices();
		this.servicesStored = servicesStored;
	}
	
	
	
}
